﻿using OAZCSharpSdk.OAZ.Plugin.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OAZCSharpSdk.OAZ.Plugin.Model
{
    public class MessageContent
    {
        /// <summary>
        /// 构建消息
        /// </summary>
        /// <param name="RobotQQ">机器人QQ</param>
        /// <param name="MsgFrom">消息来源</param>
        /// <param name="doneAc">发送消息回调</param>
        public MessageContent(long MsgFrom, Action<MessageContent> doneAc)
        {
            this.MsgFrom = MsgFrom;
            this.doneAc = doneAc;
        }

        private Action<MessageContent> doneAc { get; set; }

        private long MsgFrom { get; set; }

        private StringBuilder msg { get; set; } = new StringBuilder();

        /// <summary>
        /// 追加消息
        /// </summary>
        /// <param name="msg"></param>
        public MessageContent Append(string msg)
        {
            this.msg = this.msg.Append(msg);
            return this;
        }

        /// <summary>
        /// 追加图片
        /// </summary>
        /// <param name="path">图片本地路径</param>
        /// <returns></returns>
        public MessageContent AppendImage(string path)
        {
            this.msg = this.msg.Append(OAZ.Picture(path));
            return this;
        }

        /// <summary>
        /// 追加图片2
        /// </summary>
        /// <param name="path">图片本地路径</param>
        /// <returns></returns>
        public MessageContent AppendImage(PluginGroupMessageEventArgs e,string path)
        {
            this.msg = this.msg.Append(OAZ.Picture(e.RobotQQ, e.GroupQQ, path));
            return this;
        }

        /// <summary>
        /// 追加图片b64
        /// </summary>
        /// <param name="path">图片本地路径</param>
        /// <returns></returns>
        public MessageContent AppendImage2(PluginGroupMessageEventArgs e, string path)
        {
            this.msg = this.msg.Append(OAZ.Picture2(e.RobotQQ, e.GroupQQ, path));
            return this;
        }

        /// <summary>
        /// 追加一行
        /// </summary>
        /// <param name="msg"></param>
        public MessageContent AppendLine()
        {
            this.msg = this.msg.AppendLine();
            return this;
        }

        /// <summary>
        /// 追加消息 后面将自带换行
        /// </summary>
        /// <param name="msg"></param>
        public MessageContent AppendLine(string msg)
        {
            this.msg = this.msg.AppendLine(msg);
            return this;
        }

        /// <summary>
        /// 追加消息 后面将自带换行
        /// </summary>
        /// <param name="msg"></param>
        public MessageContent AppendFormat(string msg, params object[] args)
        {
            this.msg = this.msg.AppendFormat(msg, args);
            return this;
        }

        /// <summary>
        /// 追加AtQQ
        /// </summary>
        /// <returns></returns>
        public MessageContent AppendAtQQ() => AppendAtQQ(MsgFrom);

        /// <summary>
        /// 追加AtQQ
        /// </summary>
        /// <returns></returns>
        public MessageContent AppendAtQQ(long QQ)
        {
            this.msg = this.msg.Append(OAZ.At(QQ));
            return this;
        }

        public override string ToString()
        {
            return msg.ToString();
        }

        /// <summary>
        /// 完成消息构建,发送消息。
        /// 执行完毕后，消息会重新构建。
        /// </summary>
        public MessageContent Done()
        {
            msg = new StringBuilder(msg.ToString().TrimEnd());
            doneAc.Invoke(this);
            msg = new StringBuilder(string.Empty);
            return this;
        }

        public string Message { get; set; }
        /// <summary>
        /// 消息ID
        /// </summary>
        public long Id { get; set; }
    }
}
